const { Controller } = require("egg");

class ReportController extends Controller {
  async report() {
    // 举报用户或群
    // 1. 获取请求参数
    const { ctx, app } = this;
    const { reportType, reportConten, category } = ctx.request.body;
    const reported_id = ctx.params.reported_id;
    const id = ctx.authUser.id;
    ctx.validate({
      reportType: {
        type: "string",
        required: true,
        desc: "举报类型 user group",
      },
      reportConten: {
        type: "string",
        required: true,
        desc: "举报内容",
      },
      category: {
        type: "string",
        required: true,
        desc: "举报分类 标题",
      },
    });
    console.log(reportType, reportConten, category);
    // 2. 查询是否已经被举报过
    const report = await app.model.Report.findOne({
      where: {
        user_id: id,
        reported_id,
        status: "pending",
      },
    });
    if (report) {
      ctx.throw(400, "您已经举报过该用户,请等待管理员处理");
    }
    // 3. 创建举报记录
    const res = await app.model.Report.create({
      user_id: id,
      reported_id,
      reported_type: reportType,
      content: reportConten,
      category,
    });
    if (!res) {
      ctx.throw(500, "举报失败");
    }
    ctx.success("操作成功");
  }
  async getReportList() {
    // 获得举报列表
    const { ctx, app } = this;
    // 1. 获得举报列表
    const res = await app.model.Report.findAll();
    ctx.success(res);
  }
}
module.exports = ReportController;
